package com.qf.userfull.dict;


import java.util.Random;

public class Utils {


    /**
     * 2. 写一个方法，生成10万个随机Word对象（使用随机字符串即可），
     *
     * @return 返回 Word[]数组
     */
    public static Word[] millionWord() {
        Word[] words = new Word[100000];

        for (int i = 0; i < words.length; i++) {
            words[i] = createNewWord();
        }

        return words;
    }

    public static void printInfo(Word[] words) {
        for (int i = 0; i < words.length; i++) {
            System.out.print("第" + (i + 1) + "个单词: ");
            System.out.println(words[i].toString());
        }
    }

    /**
     * 生成一个新随机英语单词
     *
     * @return 返回一个String类型的英语单词
     */
    public static Word createNewWord() {

        Word word = new Word();
        String newEnWord = createEnOrCnWord(); // 生成英文字母
        String newChWord = createEnOrCnWord(); // 生成中文字母

        word.setEnglishWord(newEnWord);
        word.setChineseWord(newChWord);


        return word;
    }

    /**
     * 生成新英文字母(Object)
     *
     * @return 返回一个 Word对象
     */
    public static String createEnOrCnWord() {
        Random random = new Random();
        Integer length = random.nextInt(5) + 3; //设置单词长度为3-7
        String word = "";
        for (int i = 0; i < length; i++) {
            word += createRandomString();
        }

        word = word.toString();

        return word;
    }


    /**
     * 1. 写一个方法，生成随机字符串。
     *
     * @return 返回一个String 字符串,单个英语字母
     */
    public static String createRandomString() {
        String[] strs = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};

        Random random = new Random();
        Integer index = random.nextInt(26); // 生成数组下标

//        System.out.println(strs[index]);
        return strs[index];
    }
}
